<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>
        编辑库存
    </title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="../../../../css/main.css" media="all">
    <script type="text/javascript" src="../../../../lib/loading/okLoading.js"></script>
    <link rel="stylesheet" href="../../../../css/bootstrap.css">
    <link rel="stylesheet" href="../../../../lib/layui/css/layui_2.5.7.css">
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .layui-table-page select {
            height: 22px;
        }
    </style>
</head>

<body>
<div class="x-body">
    <div class="x-nav">
        <span class="layui-breadcrumb">
            <a><cite>首页</cite></a>
            <a><cite>库存查询</cite></a>
            <a><cite>编辑库存</cite></a>
        </span>
        <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
           href="javascript:location.replace(location.href);" title="刷新"><i class="layui-icon"
                                                                            style="line-height:30px">ဂ</i></a>
    </div>


    <div class="main_box">
        <div class="panel-heading">
            <h2>编辑库存</h2>
        </div>
    </div>

    <form class="layui-form x-center" id="app" lay-filter="updateInfo">

        <div class="layui-form-pane" style="margin-top: 15px;">
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span>物料号</label>
                <div class="layui-input-block">
                    <input  autocomplete="off"  type="text" lay-verify="required" class="layui-input" name="partsNum" id="partsNum"
                           placeholder="物料号" disabled>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span>计量单位</label>
                <div class="layui-input-block">
                    <input  autocomplete="off"  type="text" class="layui-input" lay-verify="required" name="partsUnits" id="partsUnits"
                           placeholder="计量单位" disabled>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span>配件名称</label>
                <div class="layui-input-block">
                    <input  autocomplete="off"  type="text" class="layui-input" lay-verify="required" name="partsName" id="partsName"
                           placeholder="配件名称">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span>配件规格</label>
                <div class="layui-input-block">
                    <input  autocomplete="off"  type="text" class="layui-input" lay-verify="required" name="specification" id="specification"
                           placeholder="配件规格">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">适用品牌</label>
                <div class="layui-input-block">
                    <input  autocomplete="off"  type="text" class="layui-input" name="applicableModels" id="applicableModels"
                           placeholder="适用品牌">
                </div>
            </div>
            <div class="layui-form-item">

                <label class="layui-form-label"><span style="color: red">*</span>适用梯型</label>
                <div class="layui-input-block">
                    <select name="applicableElevatorType" lay-verify="required" id="applicableElevatorType" lay-filter="codeType">
                        <option value="">请选择</option>
                        <option value="3">通用</option>
                        <option value="0">客梯</option>
                        <option value="1">扶梯</option>
                        <option value="2">货梯</option>
                    </select>
                </div>

            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span>安装指导价</label>
                <div class="layui-input-block">
                    <input  autocomplete="off"  type="text" class="layui-input" lay-verify="required|number" name="installGuideprice" id="installGuideprice"
                           placeholder="安装指导价">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span>指导价</label>
                <div class="layui-input-block">
                    <input  autocomplete="off"  type="text" class="layui-input" lay-verify="required|number" name="guideprice" id="guideprice" placeholder="指导价">
                </div>
            </div>
            <h3 style="text-align: left;">备注：</h3>
            <div class="layui-form-item">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-block">
                    <input  autocomplete="off"  type="text" class="layui-input" name="partsRemark" id="partsRemark" placeholder="备注">
                </div>
            </div>
            <h3 style="text-align: left;">供应商信息：</h3>
            <table class="layui-table" id="test" lay-filter="test">
            </table>

            <h3 style="text-align: left;">物料图册： (格式 jpg、jpeg、png、gif。)</h3>
            <div class="layui-upload" style="margin-top: 8px;">

                <button type="button" class="layui-btn" id="upImage">上传图片</button>
                <input  autocomplete="off"  class="layui-upload-file" type="file" accept="" name="file">
                <div class="layui-upload-list">
                    <img class="layui-upload-img" id="demo1" width="250" height="250">
                    <p id="demoText"></p>
                </div>

            </div>

            <div style="text-align: center" class="layui-form-item">
                <button class="layui-btn" type="button" lay-filter="sub" lay-submit="sub">
                    添加
                </button>
            </div>
        </div>
    </form>


</div>
</body>

<script type="text/javascript" src="../../../../js/jquery.min.js"></script>
<script type="text/javascript" src="../../../../js/myAjax.js"></script>
<script type="text/javascript" src="../../../../js/pub.js"></script>
<script type="text/javascript" src="../../../../lib/layui/layui2.5.7.js"></script>
<script type="text/javascript" src="../../../../js/vue.min.js"></script>
<!-- <script src="lib/layui/layui.js" charset="utf-8"></script> -->
<script src="../../../../js/x-layui.js" charset="utf-8"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script>
    //配置插件目录
    layui.config({
        base: '../../js/'
        , version: '1.0'
    });
</script>
<script>
    layui.use(['laydate', 'element', 'laypage', 'layer', 'form', 'upload'], function () {
        $ = layui.jquery;//jquery
        laydate = layui.laydate;//日期插件
        lement = layui.element;//面包导航
        laypage = layui.laypage;//分页
        layer = layui.layer;//弹出层
        var form = layui.form;
        okLoading.close($);
    });
</script>


<script>
    var recordId = getQueryVariable("recordId");

    const vm = new Vue({
        el: "#app",
        data: {

        },
        created() {

        },
        computed: {

        },
        methods: {

        },
        mounted() {
            var that = this;
            let request = {
                url: 'kf/parts/editParts',
                data: {
                    userId: $userId,
                    id:recordId
                },
                method: "get"
            }
            sendRequest(request, (res) => {
                var info = res.data.partsData;
                console.log(res);

                layui.use(['table', 'form', 'upload','layer'], function() {
                    var table = layui.table;
                    var form = layui.form;
                    var upload = layui.upload;

                    var picUrl = null ;
                    var oldUrl = null;

                    var codeTypeSelect = document.getElementById("applicableElevatorType");
                    for(var i=0;i<codeTypeSelect.length;i++){
                        if(codeTypeSelect[i].value==info.applicableElevatorType)
                            codeTypeSelect[i].selected = true;
                    }
                    oldUrl = res.data.partsData.partsImg;
                    if(oldUrl!=""){
                        $('#demo1').attr('src', baseHead+"/other/viewUploadFile?id="+oldUrl);
                    }

                        //表单赋值
                        form.val("updateInfo", { //updateInfo 即 class="layui-form" 所在元素属性 lay-filter="" 对应的值
                            "partsNum": info.partsNum, // "name": "value"
                            "partsUnits": info.partsUnits,
                            "partsName": info.partsName,
                            "specification": info.specification,
                            "applicableModels": info.applicableModels,
                            "installGuideprice": info.installGuideprice,
                            "guideprice": info.guideprice,
                            "partsRemark":info.partsRemark,
                        });
                    form.render();

                    //自定义验证规则
                    form.verify({
                        nikename: function(value) {
                            if (value.length < 5) {
                                return '昵称至少得5个字符啊';
                            }
                        },
                        pass: [/(.+){6,12}$/, '密码必须6到12位'],
                        repass: function(value) {
                            if ($('#L_pass').val() != $('#L_repass').val()) {
                                return '两次密码不一致';
                            }
                        }
                    });

                    //监听区选择
                    form.on('select(qu)', function(data) {
                        that.quVal = data.elem[data.elem.selectedIndex].text;
                        setTimeout(() => {
                            form.render('select');
                        }, 200);
                    });

                    //普通图片上传
                    //执行实例
                    var index = null;
                    var uploadInst = upload.render({
                        elem: '#upImage'
                        ,url: baseHead+'other/fileUpload1' //改成您自己的上传接口
                        ,before: function(obj){
                            //预读本地文件示例，不支持ie8
                            obj.preview(function(index, file, result){
                                $('#demo1').attr('src', result); //图片链接（base64）
                            });
                            index = layer.load();
                        }
                        ,done: function(res){
                            //如果上传成功
                            layer.close(index);
                            if(res.code == 0){
                                picUrl = res.data;
                            }
                            else {
                                layer.alert("上传失败，请重新上传！");
                            }
                        }
                        ,error: function(){
                            //演示失败状态，并实现重传
                            var demoText = $('#demoText');
                            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                            demoText.find('.demo-reload').on('click', function(){
                                uploadInst.upload();
                            });
                        }
                    });


                    //方法级渲染
                    var t1 = table.render({
                        elem: '#test',
                        url: $baseUrl + 'kf/parts/selectSuppileOffer',
                        where: {
                            userId: $userId,
                            id:recordId
                        },
                        headers: {
                            token: $token
                        }
                        , toolbar: '#toolbarDemo',

                        // toolbar: true,
                        cols: [
                            [
                                {
                                    title: '供应商名称',
                                    fixed: 'left',
                                    templet: function (item) {
                                        return item.supplierName;
                                    }

                                },
                                {
                                    title: '供应商联系人',
                                    templet: function (item) {
                                        return item.contact == null ? '空' : item.contact;
                                    }
                                },
                                {
                                    title: '供货商料号',
                                    templet: function (item) {
                                        return item.supplierNum == null ? '空' : item.supplierNum;
                                    }
                                },
                                {
                                    title: ' 配件报价',
                                    templet: function (item) {
                                        return item.partsOffer == null ? '空' : item.partsOffer;
                                    }
                                },
                                {
                                    title: '备注',
                                    templet: function (item) {
                                        return item.offerRemark;
                                    }

                                },
                              ]
                        ],
                        request: {
                            pageName: 'page', //页码的参数名称，默认：page
                            limitName: 'size',
                        },
                        response: {
                            statusName: 'code' //规定数据状态的字段名称，默认：code
                            ,
                            statusCode: 0 //规定成功的状态码，默认：0
                            ,
                            msgName: 'msg' //规定状态信息的字段名称，默认：msg
                            ,
                            countName: 'total' //规定数据总数的字段名称，默认：count
                            ,
                            dataName: 'data', //规定数据列表的字段名称，默认：data
                        },
                        id: 'test',
                        page: true,
                        height: 'auto',
                        even: true,
                        done: function (res, curr, count) {
                            //是否有权限或权限是否失效
                            // authorizeState();
                            // console.log(res);
                        }
                    });


                    form.on('submit(sub)',
                        function (data) {
                            if(picUrl == null){
                                picUrl = oldUrl;
                            }
                            var val = data.field;
                            val.userId = $userId;
                            val.id = recordId;

                            val.picUrl=picUrl;
                            let request = {
                                url: 'kf/parts/saveParts',
                                data: val,
                                method: "post",
                            };
                            sendRequest(request, (data) => {
                                if (data.code === 0) {
                                    layer.alert("操作成功", {
                                            icon: 6
                                        },
                                        function () {
                                            var index = parent.layer.getFrameIndex(window.name);
                                            parent.layer.close(index);
                                            parent.location.reload();

                                        });
                                } else {
                                    layer.alert(data.msg, {
                                        icon: 5
                                    })
                                }
                            });
                            return false;
                        });
                });
            })
        },


    });

</script>


</html>
